// 将整个逻辑包裹在 async 函数中
async function processTasks() {
    let tableData = $this.model.table_1763618670745_tBIbO;
    if (!tableData || tableData.length === 0) {
        return;
    }

    // 使用 for...of 循环，支持 await
    for (let i = 0; i < tableData.length; i++) {
        try {
            let bodyData = tableData[i];
            let id = $this.$util.uuid();
            let no = $this.$util.uuid();
            let headData = $this.model;

            // 处理项目类型数据
            let xmNo = "", xmName = "", yfxmNo = "", yfxmName = "";
            const taskType = headData.select_1763618264478_yHlXO;

            if (taskType === "88b3d81abfc18c0a0dffe358db029aa8") {
                xmNo = bodyData["body_1763619303580_Z5c0v"] || '';
                xmName = bodyData["body_1763619308927_OQmEy"] || '';
            }
            if (taskType === "38ffeca8a8ef9049c03d3931c8e8d92c") {
                yfxmNo = bodyData["body_1763619314177_FWnSB"] || '';
                yfxmName = bodyData["body_1763619322625_PaXyk"] || '';
            }

            // 处理日期范围
            let planTimeRange = bodyData["body_1763618722902_bOpmr"];
            let startTimeStr = "", endTimeStr = "";
            if (Array.isArray(planTimeRange) && planTimeRange.length === 2) {
                startTimeStr = planTimeRange[0] ?
                    planTimeRange[0].format("YYYY-MM-DD HH:mm:ss") : "";
                endTimeStr = planTimeRange[1] ?
                    planTimeRange[1].format("YYYY-MM-DD HH:mm:ss") : "";
            }

            // 构建保存对象（修复重复字段）
            let saveObj = {
                "bill_id": id,
                "create_user_id": headData.create_user_id,
                "create_user_name": headData.create_user_name,
                "create_dept_name": headData.create_dept_name,
                "bill_type": headData.bill_type,
                "create_dept_id": headData.create_dept_id,
                "bill_no": no,
                "create_company_name": headData.create_company_name,
                "create_date": headData.create_date,
                "number_1711439316655_41L04": 0,
                "number_1711439319768_Hzhsj": 0,
                "systemSelect_1711504973428_U0If2": headData.create_user_name,
                "systemSelect_1711504973428_U0If2_ID": headData.create_user_id,
                "number_1711505160101_Zy3nM": 0,
                "number_1711505281945_eUZOr": 0,
                "input_1711505283971_PgltR": "false",
                "number_1711505313898_96ND4": "0",
                "input_1711518795031_xvIfW": "1",
                "systemSelect_1711520339026_rFF5K": headData.create_company_id,
                "systemSelect_1711520339026_rFF5K_ID": headData.create_company_id,
                "select_1711505903223_YWk8w": headData.select_1711505903223_YWk8w,
                "input_1751171983502_E4gXx": 0,
                "selectStr_1718345029613_IWjr8": headData.selectStr_1763618295712_PrIQi || '',
                "selectStr_1718345029613_IWjr8_ID": 0,
                "input_1711439260720_6QPqX": bodyData["body_1763619851051_PPwwx"],
                "rangePicker_1711439307444_Ftzhm": [startTimeStr, endTimeStr],
                "systemSelect_1711439400905_v5lzI": bodyData["body_1763618772738_hEucH"],
                "systemSelect_1711439400905_v5lzI_ID": bodyData["body_1763618772738_hEucH_ID"],
                "systemSelect_1711439399614_qXQ72": bodyData["body_1763618737628_edMjL"],
                "systemSelect_1711439399614_qXQ72_ID": bodyData["body_1763618737628_edMjL_ID"],
                "input_1711505193468_UJHDS": bodyData["body_id"],
                "input_1711520372633_i4sGc": headData.create_date,
                "textarea_1755766219698_SuIuk": bodyData["body_1763618810450_juKHZ"],
                "input_1718345101976_ZNGth": xmNo,  // 项目编号
                "input_1718345101976_PBcTl": xmName, // 项目名称
                "input_1756173506324_x5wKf": yfxmNo, // 研发项目编号
                "input_1756173506324_R28UQ": yfxmName, // 研发项目名称
                "status":2
            };

            // 检查状态（使用 await 等待）
            let formDataH = new FormData();
            formDataH.append("billId", headData.bill_id);
            formDataH.append("tablesName", "b_pilxinzrw_h");

            let isRes = await $this.$http.post("/formsBusiness/getCommonStatus/", formDataH);

            if (isRes.data.code === 280 && isRes.data.data === 2) {
                let formData = new FormData();
                formData.append('obj', JSON.stringify(saveObj));
                formData.append("files", JSON.stringify([]));

                // 保存任务（使用 await 等待完成）
                let saveRes = await $this.$http.post("/wind/form/save/task/add", formData);

                if (saveRes.data) {
                    console.log(`第 ${i + 1} 条任务保存成功:`, saveRes.data.data);
                    $this.$message.success(`第 ${i + 1} 条任务保存成功`);
                }
            } else {
                console.warn('状态检查未通过:', isRes.data);
                $this.$message.error(`第 ${i + 1} 条任务状态检查失败`);
            }
        } catch (e) {
            $this.$message.error(`第 ${i + 1} 条任务操作失败: ${e.message}`);
            console.error('详细错误:', e);
            // 可选：是否继续执行下一个任务
            // continue; // 默认继续
            // throw e; // 中止整个流程
        }
    }

    // 全部完成后提示
    $this.$message.success('所有任务处理完成');
}

// 执行异步函数
processTasks();